﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmThemPhieuThuePhong : Form
    {
        private string _maPhong = null;
        private int _maPhieu = 0;

        public int MaPhieu
        {
            get { return _maPhieu; }
            set { _maPhieu = value; }
        }

        public string MaPhong
        {
            get { return _maPhong; }
            set { _maPhong = value; }
        }

        public frmThemPhieuThuePhong()
        {
            InitializeComponent();
        }

        public frmThemPhieuThuePhong(string MaPhong)
        {
            this.MaPhong = MaPhong;
            InitializeComponent();
        }

        private void frmThemPhieuThuePhong_Load(object sender, EventArgs e)
        {
            //load loai phong
            List<LoaiPhongDTO> dsLoaiPhong = new List<LoaiPhongDTO>();
            LoaiPhongBUS lpBus = new LoaiPhongBUS();
            dsLoaiPhong = lpBus.GetAll();
            cbxLoaiPhong.DataSource = dsLoaiPhong;
            cbxLoaiPhong.DisplayMember = "Tenloaiphong";
            cbxLoaiPhong.ValueMember = "Maloaiphong";

            //load phuong thuc thanh toan
            cbxThanhToan.SelectedIndex = 0;

            //load loai khach hang
            List<LoaiKhachHangDTO> dsLoaiKH = new List<LoaiKhachHangDTO>();
            dsLoaiKH = LoaiKhachHangBUS.LayDanhSachLoaiKhachHang();
            colLoaiKhachHang.DataSource = dsLoaiKH;
            colLoaiKhachHang.DataPropertyName = "MaLoaiKH";
            colLoaiKhachHang.DisplayMember = "TenLoaiKH";
            colLoaiKhachHang.ValueMember = "MaLoaiKH";
            colLoaiKhachHang.DefaultCellStyle.NullValue = ((LoaiKhachHangDTO)colLoaiKhachHang.Items[0]).TenLoaiKH.ToString();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbxLoaiPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            //load phong theo loai
            List<PhongDTO> dsPhong = new List<PhongDTO>();
            LoaiPhongDTO lpi = new LoaiPhongDTO();
            lpi = (LoaiPhongDTO)cbxLoaiPhong.SelectedItem;
            cbxPhong.DataSource = PhieuThuePhongBUS.LayPhongTrongTheoMaLoaiPhong(lpi.Maloaiphong);
            cbxPhong.DisplayMember = "Tenphong";
            cbxPhong.ValueMember = "Maphong";

            //thay doi so luong khach thue phong
            string MaLoaiPhong = ((LoaiPhongDTO)cbxLoaiPhong.SelectedItem).Maloaiphong;
            int sokhachtoida = PhieuThuePhongBUS.LaySoKhachToiDaTheoLoaiPhong(MaLoaiPhong);
            int rowcount = dgvDS.Rows.Count - 1;
            if (this.dgvDS.Rows.Count > 1)
            {
                if (sokhachtoida < rowcount)
                {
                    dgvDS.Rows.Clear();
                }
            }

            //them phieu thue phong da xac dinh dc phong
            if (this.MaPhong != null)
            {
                PhongInfo p = new PhongInfo();
                p = PhieuThuePhongBUS.LayThongTinPhong(this.MaPhong);
                
            }
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            string err = "";
            if (dgvDS.Rows[0].Cells["colSTT"].Value == null)
            {
                err = " - Danh sách khách hàng trống\n";
            }

            if (cbxPhong.SelectedItem == null)
            {
                errorProvider1.SetError(cbxPhong, "Chưa chọn phòng");
                err += " - Chưa chọn phòng";
            }

            if(err.Length>0)
            {
                MessageBox.Show(err, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            btnThemMoi.Enabled = false;

            //them khach hang vao bang khach hang
            List<int> dsMaKH = new List<int>();
            List<KhachHangDTO> dsKH = new List<KhachHangDTO>();
            for (int i = 0; i < this.dgvDS.Rows.Count - 1; i++)
            {
                KhachHangDTO kh = new KhachHangDTO();
                kh.HoTen = dgvDS.Rows[i].Cells["colHoTen"].Value.ToString();
                DataGridViewComboBoxCell ce = new DataGridViewComboBoxCell();
                ce = (DataGridViewComboBoxCell)dgvDS.Rows[i].Cells["colLoaiKhachHang"];
                if (ce.Value != null) { kh.LoaiKH = Int32.Parse(ce.Value.ToString()); }
                else { kh.LoaiKH = 1; }
                kh.Cmnd = dgvDS.Rows[i].Cells["colCMND"].Value.ToString();
                kh.DiaChi = dgvDS.Rows[i].Cells["colDiaChi"].Value.ToString();
                dsKH.Add(kh);
            }
            dsMaKH = KhachHangBUS.ThemKhachHang(dsKH);
            
            if (dsMaKH.Count > 0)
            {
                //Them phieu thue
                PhieuThuePhongDTO phieu = new PhieuThuePhongDTO();
                phieu.MaPhong = ((PhongDTO)cbxPhong.SelectedItem).Maphong;
                phieu.NgayBD = dtpBatDau.Value;
                phieu.NgayKT = dtpKetThuc.Value;
                phieu.TienDatCoc = txtThanhToanTruoc.Enabled? Int32.Parse(txtThanhToanTruoc.Text):0;
                this.MaPhieu = PhieuThuePhongBUS.ThemPhieuThuePhong(phieu, dsMaKH);
            }

            //da thue phong, xoa phong trong nay.
            this.MaPhong = ((PhongDTO)cbxPhong.SelectedItem).Maphong;
        }

        private void dgvDS_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            string MaLoaiPhong = ((LoaiPhongDTO)cbxLoaiPhong.SelectedItem).Maloaiphong;
            int sokhachtoida = PhieuThuePhongBUS.LaySoKhachToiDaTheoLoaiPhong(MaLoaiPhong);
            if (dgvDS.Rows.Count > sokhachtoida + 1)
            {
                MessageBox.Show("Loại phòng này chỉ cho phép ở tối đa " + sokhachtoida.ToString() + " người.",
                    "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.dgvDS.Rows.RemoveAt(e.Row.Index - 1);
                return;
            }
            int i = e.Row.Index;
            dgvDS.Rows[i - 1].Cells[0].Value = i;
        }

        private void txtThanhToanTruoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(!Char.IsDigit(e.KeyChar) && !e.KeyChar.Equals(',') && !e.KeyChar.Equals('.') && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Dữ liệu nhập phải là kiểu số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Handled = true;
            }
        }

        private void cbxThanhToan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbxThanhToan.SelectedIndex != 0)
            {
                txtThanhToanTruoc.Enabled = true;
            }
            else
            {
                txtThanhToanTruoc.Enabled = false;
            }

            if(cbxPhong.SelectedIndex == 1)
            {
                string MaLoaiPhong = ((LoaiPhongDTO)cbxLoaiPhong.SelectedItem).Maloaiphong;
                txtThanhToanTruoc.Text = (PhieuThuePhongBUS.LayGiaPhongTheoLoaiPhong(MaLoaiPhong)).ToString();
            }
        }
    }
}
